using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace KFGame
{
    public class KFSteeringWheelAligmentView : KFBackViewManager
    {
        // Start is called before the first frame update

        public string tips_1 = "请按”开始校正“进行校正";
        public string tips_2 = "请把方向盘转到最左边";
        public string tips_3 = "请把方向盘转到最右边";
        public string tips_4 = "请把方向盘回正放开手,然后确定”完成校正“ ";

        public string btn_1 = "开始校正";
        public string btn_2 = "完成校正";

        //是否开始校正
        [HideInInspector]
        public bool isAligment = false;
        //提示框
        public Text Tips;
        //最左边
        public Text leftNum;
        //最右边
        public Text rigntNum;
        //中心数值
        public Text centerNum;
        //按钮字
        public Text okbtnfont;
        //方向盘
        public GameObject steeringwheel;


        //最大值
        int maxNumber = 255;
        //最小值
        int minNumber = 0;

        //当前的数值
        public int currNumber = 125;

        //最左边
        int leftNumer = -1;
        //最右边
        int rightNumber = -1;
        //中心值
        int centerNumber = -1;

        //步骤
        int step = 0;
        //步骤时间
        float steptime = 3f;

        //设置管理工具
        KFSetting setting;

        void Start()
        {
            setting = KFSetting.instantiation();
            initdatas();

            startAligment();
        }

        //初始化数据
        public void initdatas()
        {
            int runber = setting.ItemIntegre("KFSteeringWheel_Right");
            int lunber = setting.ItemIntegre("KFSteeringWheel_Left");
            int cunber = setting.ItemIntegre("KFSteeringWheel_Center");

            rigntNum.text = "" + runber;
            leftNum.text = "" + lunber;
            centerNum.text = "" + cunber;
        }

        public void saveDatas()
        {
            setting.SetItem("KFSteeringWheel_Right", rigntNum.text);
            setting.SetItem("KFSteeringWheel_Left", leftNum.text);
            setting.SetItem("KFSteeringWheel_Center", centerNum.text);
            setting.save();
        }

        public void doNextStep()
        {
            if (!isAligment)
                return;

            step++;

            if (step > 3)
                step = 3;

            startAligment(step);
            StartCoroutine(StepTime());
        }
        IEnumerator StepTime()
        {
            yield return new WaitForSeconds(steptime);
            doNextStep();
        }

        /// <summary>
        /// 运行步骤
        /// </summary>
        public void onStep()
        {
            //左边
            if (step == 1)
            {
                if (leftNumer < 0)
                    leftNumer = currNumber;
                if (leftNumer > currNumber)
                    leftNumer = currNumber;
                if (leftNumer >= 0)
                    leftNum.text = "" + leftNumer;
            }
            //最右边
            if (step == 2)
            {
                if (rightNumber < 0)
                    rightNumber = currNumber;
                if (rightNumber < currNumber)
                    rightNumber = currNumber;
                if (rightNumber > 0)
                    rigntNum.text = "" + rightNumber;
            }
            //最中间
            if (step == 3)
            {
                centerNumber = currNumber;
                centerNum.text = "" + centerNumber;
            }

        }

        /// <summary>
        /// 方向盘转动
        /// </summary>
        void restSteeringWheel()
        {
            float numb = (float)currNumber / (float)(maxNumber - minNumber);
            float ang = (0.5f - numb) * 2 * 90;
            Vector3 sang = new Vector3(0, 0, ang);
            steeringwheel.transform.rotation = Quaternion.Euler(sang);

        }

        // Update is called once per frame
        void Update()
        {
            if (isAligment)
            {
                restSteeringWheel();
                onStep();
            }
        }
        void initStart()
        {
            okbtnfont.text = btn_1;
            Tips.text = tips_1;
        }

        /// <summary>
        /// 开始校正步骤
        /// </summary>
        void startAligment(int index = 0)
        {
            if (index == 0)
            {
                okbtnfont.text = btn_1;
                Tips.text = tips_1;
            }
            if (index == 1)
            {
                Tips.text = tips_2;
                okbtnfont.text = btn_2;
            }
            if (index == 2)
            {
                Tips.text = tips_3;
                okbtnfont.text = btn_2;
            }
            if (index == 3)
            {
                Tips.text = tips_4;
                okbtnfont.text = btn_2;
            }

        }

        /// <summary>
        /// 按钮触发事件
        /// </summary>
        public void onOKBtn()
        {
            //己开始 ，再点关闭
            if (isAligment)
            {
                isAligment = false;
                step = 0;
                startAligment(step);
                save();
                return;
            }

            isAligment = true;
            step = 0;
            doNextStep();

        }

        /// <summary>
        /// 按钮触发事件
        /// </summary>
        public void toClose()
        {
            close();
        }

        // save datas to setting
        void save()
        {
            saveDatas();
        }
    }
}
